import tushare as ts
import pandas as pd
import common.base_profile as pf


# 设置你的 Tushare token
ts.set_token(pf.token)

# 初始化接口
pro = ts.pro_api()

def fetch_and_save_stock_basic(output_csv='../data/stock_basic.csv', output_excel='index_basic.xlsx'):
    try:
        # 调用接口
        df = pro.stock_basic()

        # 写入 CSV 文件
        df.to_csv(output_csv, index=False, encoding='utf-8-sig')
        print(f"数据已成功写入 {output_csv}")

        # # 写入 Excel 文件
        # df.to_excel(output_excel, index=False)
        # print(f"数据已成功写入 {output_excel}")

    except Exception as e:
        print("发生错误：", str(e))

if __name__ == '__main__':
    fetch_and_save_stock_basic()